package com.djf.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.djf.entity.TaskInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 任务信息Mapper接口
 * 
 * @author djf
 * @since 2024-01-01
 */

public interface TaskInfoMapper extends BaseMapper<TaskInfo> {
    
    /**
     * 根据任务ID查询
     */
    @Select("SELECT * FROM task_info WHERE task_id = #{taskId} AND deleted = 0")
    TaskInfo selectByTaskId(@Param("taskId") String taskId);
    
    /**
     * 根据流程实例ID查询任务列表
     */
    @Select("SELECT * FROM task_info WHERE process_instance_id = #{processInstanceId} AND deleted = 0 ORDER BY created_time DESC")
    List<TaskInfo> selectByProcessInstanceId(@Param("processInstanceId") String processInstanceId);
    
    /**
     * 根据分配人查询待办任务
     */
    @Select("SELECT * FROM task_info WHERE assignee = #{assignee} AND status = 'CREATED' AND deleted = 0 ORDER BY created_time DESC")
    List<TaskInfo> selectTodoTasksByAssignee(@Param("assignee") String assignee);
    
    /**
     * 根据候选用户查询候选任务
     */
    @Select("SELECT * FROM task_info WHERE (candidate_users LIKE CONCAT('%', #{userId}, '%') OR candidate_groups LIKE CONCAT('%', #{groupId}, '%')) AND assignee IS NULL AND status = 'CREATED' AND deleted = 0 ORDER BY created_time DESC")
    List<TaskInfo> selectCandidateTasksByUser(@Param("userId") String userId, @Param("groupId") String groupId);
    
    /**
     * 根据状态查询任务
     */
    @Select("SELECT * FROM task_info WHERE status = #{status} AND deleted = 0 ORDER BY created_time DESC")
    List<TaskInfo> selectByStatus(@Param("status") String status);
}